<script setup lang="ts">
import { nextTick, onMounted, ref, toRefs, watch } from 'vue';

import {
  ElCoin,
  ElDArrowLeft,
  ElEdit,
  ElInfoFilled,
  ElLink,
  ElSetting,
} from '@vben/icons';

import { StorageManager } from '@vben-core/shared/cache';

import {
  ElCard,
  ElCollapse,
  ElCollapseItem,
  ElDescriptions,
  ElDescriptionsItem,
  ElDivider,
  ElEmpty,
  ElIcon,
  ElInput,
  ElPopover,
  ElScrollbar,
  ElTag,
  ElTooltip,
} from 'element-plus';

import { useSceneStore } from '#/store';

const props = defineProps({
  isSceneRenameing: {
    type: Boolean,
    default: false,
  },
});

const emit = defineEmits(['closeSettingPannel', 'switchMap']);

const storageManager = new StorageManager({
  prefix: '',
  storageType: 'sessionStorage',
});

const { isSceneRenameing } = toRefs(props);
const sceneStore = useSceneStore();
const isRenaming = ref(false);
const activeNames = ref(['1']);
// const filterText = ref('')
// const selectedTool = reactive({} as any)

const layers = ref([] as any[]);

const closeSettingPannel = () => {
  emit('closeSettingPannel');
};

const renameScene = () => {
  isRenaming.value = true;
  nextTick(() => {
    const renameInput = document.querySelector('#renaming') as any;
    // console.log('dom', input);
    renameInput.focus();
  });
};
onMounted(() => {
  layers.value = sceneStore.currentSceneConfig.sceneLayerGroup;

  watch(
    isSceneRenameing,
    () => {
      if (isSceneRenameing.value) {
        renameScene();
      }
    },
    {
      immediate: true,
    },
  );
});
</script>

<template>
  <div class="all">
    <div class="custom-setting-container">
      <div class="header">
        <div class="btnCon">
          <ElIcon
            size="20"
            class="backBtn"
            title="收起设置面板"
            @click="closeSettingPannel"
          >
            <ElDArrowLeft />
          </ElIcon>
        </div>
        <div class="title">
          <ElIcon size="21"> <ElSetting /> </ElIcon>&nbsp;
          <span style="margin-right: 40px">场景属性</span>
        </div>
      </div>
      <ElDivider
        border-style="hidden"
        style="margin-top: 0; margin-bottom: 0"
      />
      <div class="setting-body">
        <ElCollapse accordion v-model="activeNames">
          <ElCollapseItem name="1">
            <template #title>
              <span class="item-title"> 场景信息 </span>
              <ElIcon class="header-icon">
                <ElInfoFilled />
              </ElIcon>
            </template>
            <div class="scene-info">
              <ElDescriptions :column="1" border size="large">
                <ElDescriptionsItem
                  label="场景名称"
                  label-align="center"
                  align="center"
                  :min-width="90"
                >
                  <div class="dom-center">
                    <ElTooltip
                      :content="sceneStore.editingScene.sceneName"
                      placement="top"
                      v-if="!isRenaming"
                    >
                      <span class="scene-info-item">{{
                        sceneStore.editingScene.sceneName
                      }}</span>
                    </ElTooltip>
                    <ElInput
                      class="renaming-input"
                      v-model="sceneStore.editingScene.sceneName"
                      v-else
                      clearable
                      @blur="isRenaming = false"
                      id="renaming"
                    />
                    <ElTooltip content="编辑" placement="top">
                      <ElIcon @click="renameScene" style="cursor: pointer">
                        <ElEdit />
                      </ElIcon>
                    </ElTooltip>
                  </div>
                </ElDescriptionsItem>
                <ElDescriptionsItem
                  label="场景类型"
                  label-align="center"
                  align="center"
                  :min-width="90"
                >
                  <div class="dom-center">
                    <ElTag size="default">
                      {{ sceneStore.editingScene.sceneType }}
                    </ElTag>
                    &nbsp;
                    <ElPopover :width="300" placement="right">
                      <template #reference>
                        <ElIcon
                          color="#3f9eff"
                          size="16"
                          style="cursor: pointer"
                        >
                          <ElInfoFilled />
                        </ElIcon>
                      </template>
                      <template #default>
                        <div
                          style="
                            display: flex;
                            flex-direction: column;
                            gap: 16px;
                          "
                        >
                          <div>
                            <p style="margin: 0 0 10px; font-weight: 700">
                              GDV
                            </p>
                            <p
                              style="
                                margin: 0;
                                font-size: 14px;
                                color: var(--el-color-info);
                              "
                            >
                              Geographical Data Visualisation Scene
                            </p>
                            <p style="margin: 0">地理数据可视化场景</p>
                          </div>
                        </div>
                      </template>
                    </ElPopover>
                  </div>
                </ElDescriptionsItem>
                <ElDescriptionsItem
                  label="创建者"
                  :span="2"
                  label-align="center"
                  :min-width="90"
                  align="center"
                >
                  <span class="scene-info-item">{{
                    storageManager.getItem('username')
                  }}</span>
                </ElDescriptionsItem>
              </ElDescriptions>
            </div>
          </ElCollapseItem>
          <ElCollapseItem name="2" class="dataSource-collapse">
            <template #title>
              <span class="item-title">场景数据源</span>
            </template>
            <ElDivider style="margin-top: 0; margin-bottom: 0" />
            <ElEmpty
              description="您还未添加数据源"
              v-if="sceneStore.currentSceneConfig.sources.length === 0"
            />
            <ElScrollbar style="width: 100%; height: 100%" v-else>
              <ElCard
                v-for="(dataSource, index) in sceneStore.currentSceneConfig
                  .sources"
                :body-style="{ padding: '0px' }"
                class="dataSource"
                :key="index"
              >
                <template #header>
                  <div class="source-header">
                    <div class="source-title">
                      <span
                        v-if="!dataSource.isRenaming"
                        class="title-text"
                        :title="dataSource.sourceName"
                        >{{ dataSource.sourceName }}
                      </span>
                      <ElInput
                        v-model="dataSource.sourceName"
                        v-else
                        clearable
                        @blur="dataSource.isRenaming = false"
                        id="renaming"
                      />
                    </div>
                  </div>
                </template>
                <div class="source-info">
                  <div>
                    数据类别：<ElTag class="ml-2" type="warning">
                      {{
                        dataSource.sourceType === 'vector'
                          ? 'MVT'
                          : dataSource.sourceType.toUpperCase()
                      }}
                    </ElTag>
                  </div>
                  <div>
                    类型：<ElTag class="ml-2" type="success">
                      {{ dataSource.geoType ? dataSource.geoType : 'RASTER' }}
                    </ElTag>
                  </div>
                </div>
                <ElDivider
                  style="margin-top: 2px; margin-bottom: 2px"
                  border-style="hidden"
                />
                <div class="source-link">
                  <div style="display: flex; align-items: center">
                    <ElIcon> <ElCoin /> </ElIcon>&nbsp;
                    <div class="source-link-item">
                      <span>
                        服务资源：
                        <span
                          class="data-info"
                          :title="dataSource.serviceSource"
                          >{{ dataSource.serviceSource }}
                        </span>
                      </span>
                    </div>
                  </div>
                  <ElDivider
                    style="margin-top: 2px; margin-bottom: 2px"
                    border-style="hidden"
                  />
                  <div style="display: flex; align-items: center">
                    <ElIcon> <ElLink /> </ElIcon>&nbsp;
                    <div class="source-link-item">
                      <span>
                        所属用户：
                        <span
                          class="data-info"
                          :title="
                            storageManager.getItem<string>('username') as string
                          "
                        >
                          {{ storageManager.getItem('username') }}
                        </span>
                      </span>
                    </div>
                  </div>
                </div>
              </ElCard>
            </ElScrollbar>
          </ElCollapseItem>
        </ElCollapse>
      </div>
    </div>
  </div>
</template>

<style scoped>
.all {
  /* display: flex;
  background-color: rgb(255, 255, 255);
  height: 88.5vh;
  width: 371px;
  margin-left: v-bind(menuWidth);
  position: fixed;
  z-index: 999; */
  display: flex;
  width: 100%;
  height: 100%;
  background-color: rgb(255 255 255);
}

.custom-setting-container {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100%;
}

.icon {
  width: 1em;
  height: 1em;
  overflow: hidden;
  vertical-align: -0.15em;
  fill: currentcolor;
}

.a {
  color: blue;
}

.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 55px;
}

.title {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  padding: 10px;
  margin-right: 40px;
  font-size: 18px;
  font-weight: bold;
  color: rgb(73 70 70);
}

.setting-body {
  height: 60vh;
  padding: 0 10px;
}

.scene-info {
  width: 100%;
}

.dom-center {
  display: flex;
  align-items: center;
  justify-content: center;
}

.item-title {
  font-size: 16px;
  font-weight: 700;
}

:deep(.el-collapse-item__header:hover) {
  color: #3f9eff;
}

.scene-info-item {
  display: block;
  width: 180px;

  /* 禁止换行 */
  overflow: hidden;

  /* 隐藏超出部分 */
  text-overflow: ellipsis;
  white-space: nowrap;

  /* 使用省略号显示超出内容 */
}

:deep(.renaming-input .el-input__inner) {
  /* width: 140px; */
  font-size: 14px;
}

.btnCon {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 20%;
  padding: 10px;
}

.treeCon {
  padding: 20px;
}

.backBtn:hover {
  cursor: pointer;
  transform: scale(1.05);
}

.dataSource {
  width: 98%;
  height: auto;

  /* margin: 0px 0px 2px 0px; */
  margin-bottom: 2px;
}

.dataSource:hover {
  cursor: pointer;
  background-color: #ddd;
}

.source-header {
  display: flex;
  justify-content: space-between;

  /* width: 100%; */
  padding: 0 10px;
  padding-right: 5px;
}

.source-title {
  width: 72%;

  /* 禁止换行 */
  overflow: hidden;

  /* 隐藏超出部分 */
  text-overflow: ellipsis;

  /* 超出长度换行 */

  /* word-wrap: break-word; */
  white-space: nowrap;

  /* 使用省略号显示超出内容 */
}

.source-btn {
  width: 26%;
}

.title-text {
  font-size: 17px;
  font-weight: 700;
}

.source-info {
  display: flex;
  justify-content: space-between;
  padding: 5px 10px;
  font-size: 14px;
}

.data-info {
  font-size: 14px;
  color: #999;
}

.source-link {
  width: 95%;
  padding: 0 10px 5px;
  font-size: 14px;

  /* color: #409EFF; */
}

.source-link-item {
  /* 禁止换行 */
  overflow: hidden;

  /* 隐藏超出部分 */
  text-overflow: ellipsis;
  white-space: nowrap;

  /* 使用省略号显示超出内容 */
}

:deep(.dataSource-collapse .el-collapse-item__content) {
  /* height: 10%; */
  height: 700px;
}
</style>
